package com.ruoyi.common.utils.wechat;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Data
@Configuration
@ConfigurationProperties(prefix = "wechat")
public class WechatProperties {

    private String appId;


    private String appSecret;

    /**
     * 商户Id
     */
    private String mchId;

    /**
     * 商户证书序列号
     */
    private String mchCertNo;

    /**
     * 商户证书私钥
     */
    private String apiClientKey;

    /**
     * v3支付密钥
     */
    private String apiV3Key;

    /**
     * 支付成功回调地址
     */
    private String payNotifyUrl;

    /**
     * 退款成功后回调地址
     */
    private String refundNotifyUrl;

    /**
     * 获取openid地址
     */
    private String openIdUrl;

    /**
     *  获取accessToken地址
     */
    private String accessTokenUrl;

    private String phoneNumberUrl;

    /**
     * 微信支付配置对象
     */
    public static RSAAutoCertificateConfig certificateConfig;

    /**
     * 退款服务
     */
    public static RefundService refundService;


    @Autowired
    RedisCache redisCache;



    // 使用自动更新平台证书的RSA配置
    // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
    @Bean
    public void initializeConfig(){
        certificateConfig = new RSAAutoCertificateConfig.Builder()
                .merchantId(mchId)
                .privateKeyFromPath(apiClientKey)
                .merchantSerialNumber(mchCertNo)
                .apiV3Key(apiV3Key)
                .build();

        // 创建退款对象
        refundService = new RefundService.Builder().config(certificateConfig).build();
    }

    @Bean
    public void initAccessToken(){
        getAccessToken();
    }


    public String getAccessToken(){
        String accessToken = redisCache.getCacheObject("access_token");
        System.out.println("redis中accessToken:" + accessToken);
        // 已过期
        if(!redisCache.hasKey("access_token")){
            System.out.println("没有accesstoken");
            String fullAccessTokenUrl = String.format(accessTokenUrl, appId, appSecret);
            System.out.println("accessToken地址" + fullAccessTokenUrl);
            String result = HttpUtils.sendGet(fullAccessTokenUrl);
            JSONObject jsonObject = JSONObject.parseObject(result);
            System.out.println("请求accessToken解析的数据" + jsonObject);
            accessToken = (String)jsonObject.get("access_token");
            System.out.println("本次accessToken" + accessToken);
            Integer expiresIn = (Integer)jsonObject.get("expires_in") ;
            System.out.println("过期时间" + expiresIn);
            redisCache.setCacheObject("access_token", accessToken, expiresIn, TimeUnit.SECONDS);
        }
        return accessToken;
    }
}
